package nc

import (
	"github.com/project-flogo/core/data/coerce"
)

// Settings settings
type Settings struct {
	Network string `md:"network"`       // The network type
	Host    string `md:"host"`          // The host name or IP for TCP server.
	Port    string `md:"port,required"` // The port to listen on
}

// Input input data
type Input struct {
	Data []byte `md:"data,required" json:"data,omitempty"`
}

// ToMap input to map
func (r *Input) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"data": r.Data,
	}
}

// FromMap input from map
func (r *Input) FromMap(values map[string]interface{}) error {
	data, err := coerce.ToBytes(values["data"])
	if err != nil {
		return err
	}
	r.Data = data
	return nil
}

// Output output
type Output struct {
	Len int    `md:"len,required" json:"len,omitempty"`
	Err string `md:"err" json:"err"`
}

// ToMap output to map
func (r *Output) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"len": r.Len,
		"err": r.Err,
	}
}

// FromMap output from map
func (r *Output) FromMap(values map[string]interface{}) error {
	len, err := coerce.ToInt(values["len"])
	if err != nil {
		return err
	}
	r.Len = len

	werr, err := coerce.ToString(values["err"])
	if err != nil {
		return err
	}
	r.Err = werr

	return nil
}
